//
//  AudioPublishViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/11/9.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "AudioPublishViewController.h"
#import "AudioPublishBottom.h"
#import "AudioPublishTextViewBottom.h"
@interface AudioPublishViewController ()

@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UIProgressView *uploadProgressView;
@property(nonatomic,strong)UILabel *progressingLabel;
@property(nonatomic,strong)UILabel *sizeLabel;
@end

@implementation AudioPublishViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setRightButton];
    [self setupUI];
    // Do any additional setup after loading the view.
}
-(void)setRightButton{
    self.navigationItem.title = @"音频发布";
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, scaleLeftAndRight(50), 44);
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, scaleLeftAndRight(-20));
    [button setTitle:@"发布" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, scaleLeftAndRight(50), 44);
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    leftBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, scaleLeftAndRight(20));
    [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftButtonItem;
}
-(void)setupUI{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64)];
    self.scrollView.bounces = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(WIDTH, scaleTopAndBottom(1100));
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, scaleTopAndBottom(1100))];
    bgView.backgroundColor = [UIColor whiteColor];
        [self.scrollView addSubview:bgView];
    UILabel *coverPostLabel = [[UILabel alloc] init];
    NSString *coverStr = @"封面上传 *必填";
    coverPostLabel.attributedText = [self labelTextBack:coverStr];
    [bgView addSubview:coverPostLabel];
    [coverPostLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(scaleLeftAndRight(10));
      make.right.equalTo(bgView.mas_right).offset(scaleLeftAndRight(-10));
        make.top.equalTo(bgView.mas_top);
        make.height.mas_equalTo(scaleTopAndBottom(44));
    }];
    UIView *pictureView = [[UIView alloc] init];
    pictureView.backgroundColor = [UIColor greenColor];
    [bgView addSubview:pictureView];
    [pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(scaleLeftAndRight(10));
        make.right.equalTo(bgView.mas_right).offset(scaleLeftAndRight(-10));
        make.top.equalTo(coverPostLabel.mas_bottom);
        make.height.mas_equalTo(scaleTopAndBottom(210));
    }];
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = COLOR_HEX(0xCCCCCC);
    [bgView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(bgView);
        make.height.mas_equalTo(scaleTopAndBottom(5));
        make.top.equalTo(pictureView.mas_bottom).offset(scaleTopAndBottom(16));
    }];
    UILabel *videoPostLabel = [[UILabel alloc] init];
    NSString *videoStr = @"视频上传 *必填";
    videoPostLabel.attributedText = [self labelTextBack:videoStr];
    [bgView addSubview:videoPostLabel];
    [videoPostLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(scaleLeftAndRight(10));
        make.right.equalTo(bgView.mas_right).offset(scaleLeftAndRight(-10));
        make.top.equalTo(lineView.mas_bottom);
        make.height.mas_equalTo(scaleTopAndBottom(44));
    }];
    UIButton *chooseVideoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    chooseVideoBtn.frame = CGRectMake(0, 0, scaleLeftAndRight(190), scaleTopAndBottom(35));
    [chooseVideoBtn setTitle:@"请选择要上传的音频" forState:UIControlStateNormal];
    [chooseVideoBtn setTitleColor:COLOR_HEX(0xC0C0C0) forState:UIControlStateNormal];
    chooseVideoBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self addBorderToLayer:chooseVideoBtn];
    [bgView addSubview:chooseVideoBtn];
    [chooseVideoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(scaleLeftAndRight(10));
        make.top.equalTo(videoPostLabel.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(scaleLeftAndRight(190), scaleTopAndBottom(35)));

    }];
    _uploadProgressView = [[UIProgressView alloc] init];
    _uploadProgressView.progressTintColor = COLOR_HEX(0xFFC921);
    _uploadProgressView.trackTintColor = COLOR_HEX(0xD9D9D9);
    _uploadProgressView.progress = 0.5;
    [bgView addSubview:_uploadProgressView];
    [_uploadProgressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(chooseVideoBtn.mas_bottom).offset(scaleTopAndBottom(37));
        make.height.mas_equalTo(scaleTopAndBottom(2));
        make.left.equalTo(bgView.mas_left).offset(scaleLeftAndRight(10));
        make.width.mas_equalTo(scaleLeftAndRight(220));
    }];
    self.progressingLabel = [[UILabel alloc] init];
    self.progressingLabel.text = @"已上传50%";
    self.progressingLabel.font = [UIFont systemFontOfSize:12];
    self.progressingLabel.textColor = COLOR_HEX(0xFFC921);
    [bgView addSubview:self.progressingLabel];
    [self.progressingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.uploadProgressView.mas_left);
        make.bottom.equalTo(self.uploadProgressView.mas_bottom).offset(scaleTopAndBottom(-5));
        make.height.mas_equalTo(scaleTopAndBottom(12));
        
    }];
    self.sizeLabel = [[UILabel alloc] init];
    self.sizeLabel.text = @"50M/100M";
    self.sizeLabel.textAlignment = NSTextAlignmentRight;
    self.sizeLabel.textColor = COLOR_HEX(0xADADAD);
    self.sizeLabel.font = [UIFont systemFontOfSize:12];
    [bgView addSubview:self.sizeLabel];
    [self.sizeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.uploadProgressView.mas_right);
        make.bottom.equalTo(self.uploadProgressView.mas_bottom).offset(scaleTopAndBottom(-5));
        make.height.mas_equalTo(scaleTopAndBottom(12));
        make.left.equalTo(self.progressingLabel.mas_right);
        make.width.equalTo(@[self.progressingLabel]);
    }];
    UIView *filamentView = [[UIView alloc] init];
    filamentView.backgroundColor = COLOR_HEX(0x121212);
    [bgView addSubview:filamentView];
    [filamentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(bgView);
        make.height.mas_equalTo(scaleTopAndBottom(0.5));
    make.top.equalTo(self.uploadProgressView.mas_bottom).offset(scaleTopAndBottom(20));
    }];
    UILabel *remindLabel = [[UILabel alloc] init];
    remindLabel.text = @"提示";
    remindLabel.textAlignment = NSTextAlignmentCenter;
    remindLabel.textColor = [UIColor whiteColor];
    remindLabel.backgroundColor = COLOR_HEX(0xFFC921);
    remindLabel.font = [UIFont systemFontOfSize:12];
    [bgView addSubview:remindLabel];
    [remindLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(scaleLeftAndRight(10));
        make.size.mas_equalTo(CGSizeMake(scaleLeftAndRight(30), scaleTopAndBottom(18)));
        make.top.equalTo(filamentView.mas_bottom).offset(scaleTopAndBottom(26));
    }];
    UIImageView *arrowImageView = [[UIImageView alloc] init];
    arrowImageView.backgroundColor = [UIColor redColor];
    [bgView addSubview:arrowImageView];
    [arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(filamentView.mas_bottom).offset(scaleTopAndBottom(26)); make.right.equalTo(bgView.mas_right).offset(scaleLeftAndRight(-10));
        make.size.mas_equalTo(CGSizeMake(scaleLeftAndRight(10), scaleTopAndBottom(18)));
        
    }];
    UILabel *introLabel = [[UILabel alloc] init];
    introLabel.textColor = COLOR_HEX(0x121212);
    NSMutableAttributedString *atter = [self backFormatterAndStatement:@"上传作品所支持格式在这里，以及《原创声明》"];
    introLabel.attributedText = atter;
    introLabel.font = [UIFont systemFontOfSize:12];
    [bgView addSubview:introLabel];
    [introLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(filamentView.mas_bottom);
        make.height.mas_equalTo(scaleTopAndBottom(35));
        make.left.equalTo(remindLabel.mas_right).offset(scaleLeftAndRight(10));
        make.right.equalTo(arrowImageView.mas_left).offset(scaleLeftAndRight(-10));
    }];
    [remindLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(introLabel);
    }];
    [arrowImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(introLabel);
    }];
    UILabel *exploreIntroLabel = [[UILabel alloc] init];
    exploreIntroLabel.textColor = COLOR_HEX(0x999999);
    exploreIntroLabel.font = [UIFont systemFontOfSize:11];
    exploreIntroLabel.backgroundColor = COLOR_HEX(0xD9D9D9);
    exploreIntroLabel.textAlignment = NSTextAlignmentCenter;
    CGFloat imgH =exploreIntroLabel.font.pointSize;
    CGFloat imgW = ([UIImage imageNamed:@"instilocal"].size.width / [UIImage imageNamed:@"instilocal"].size.height) * imgH;
     CGFloat textPaddingTop = (exploreIntroLabel.font.lineHeight - exploreIntroLabel.font.pointSize) / 2;
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@" 作品在密探--听故事板块展示 "];
    NSTextAttachment *attach = [[NSTextAttachment alloc] init];
    attach.image = [UIImage imageNamed:@"instilocal"];
    attach.bounds = CGRectMake(0, -textPaddingTop , imgW, imgH);
    NSAttributedString *strinattach = [NSAttributedString attributedStringWithAttachment:attach];
    [attri insertAttributedString:strinattach atIndex:0];
    NSAttributedString *strinattach1 = [NSAttributedString attributedStringWithAttachment:attach];
    [attri insertAttributedString:strinattach1 atIndex:attri.length];
    exploreIntroLabel.attributedText = attri;
    [bgView addSubview:exploreIntroLabel];
    [exploreIntroLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(bgView);
        make.height.mas_equalTo(scaleTopAndBottom(scaleTopAndBottom(40)));
        make.top.equalTo(introLabel.mas_bottom);
    }];
    AudioPublishBottom *view1 = [AudioPublishBottom initWithaddView:bgView AndAboveView:exploreIntroLabel AndTitle:@"类别" AndPlaceholder:@"选择作品所属类别" AndShowMustToStar:YES];
    AudioPublishBottom *view2 = [AudioPublishBottom initWithaddView:bgView AndAboveView:view1 AndTitle:@"年龄段" AndPlaceholder:@"选择作品所属年龄段" AndShowMustToStar:YES];
    AudioPublishTextViewBottom *view3 = [AudioPublishTextViewBottom initWithaddView:bgView AndAboveView:view2 AndTitle:@"作品名称" AndPlaceholder:@"编辑作品名称" AndArticalLength:@"0/30" AndViewHeight:scaleTopAndBottom(108) AndShowMustToStar:YES];
    AudioPublishTextViewBottom *view4 = [AudioPublishTextViewBottom initWithaddView:bgView AndAboveView:view3 AndTitle:@"作品简介" AndPlaceholder:@"简单介绍作品" AndArticalLength:@"0/100" AndViewHeight:scaleTopAndBottom(160) AndShowMustToStar:NO];
    AudioPublishTextViewBottom *view5 = [AudioPublishTextViewBottom initWithaddView:bgView AndAboveView:view4 AndTitle:@"小贴士" AndPlaceholder:@"给作品做个小总结吧" AndArticalLength:@"0/100" AndViewHeight:scaleTopAndBottom(160) AndShowMustToStar:NO];
    UIView *lasBottomView = [[UIView alloc] init];
    lasBottomView.backgroundColor = [UIColor lightGrayColor];
    [bgView addSubview:lasBottomView];
    [lasBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(bgView);
        make.top.equalTo(view5.mas_bottom);
    }];
    UILabel *lastBottomLabel = [[UILabel alloc] init];
    lastBottomLabel.text = @"我是有底线的";
    lastBottomLabel.textColor = COLOR_HEX(0xB2B2B2);
    lastBottomLabel.textAlignment = NSTextAlignmentCenter;
    lastBottomLabel.font = [UIFont systemFontOfSize:10];
    NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:10.f]};
    CGSize labelSize = [lastBottomLabel.text boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
    [lasBottomView addSubview:lastBottomLabel];
    [lastBottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(lasBottomView.mas_centerX);
        make.bottom.equalTo(lasBottomView.mas_bottom).offset(scaleTopAndBottom(-25));
        make.top.equalTo(lasBottomView.mas_top).offset(scaleTopAndBottom(17));
        make.width.mas_equalTo(scaleLeftAndRight(80));
    }];
    UIView *bottomLineLeftView = [[UIView alloc] init];
    bottomLineLeftView.backgroundColor = COLOR_HEX(0xDBDBDB);
    [lasBottomView addSubview:bottomLineLeftView];
    [bottomLineLeftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lasBottomView.mas_left).offset(scaleLeftAndRight(41));
        make.centerY.equalTo(lastBottomLabel.mas_centerY);
        make.right.equalTo(lastBottomLabel.mas_left).offset(scaleLeftAndRight(0));
        make.height.mas_equalTo(1);
    }];
    UIView *bottomLineRightView = [[UIView alloc] init];
    bottomLineRightView.backgroundColor = COLOR_HEX(0xDBDBDB);
    [lasBottomView addSubview:bottomLineRightView];
    [bottomLineRightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lasBottomView.mas_right).offset(scaleLeftAndRight(-41));
        make.centerY.equalTo(lastBottomLabel.mas_centerY);
        make.left.equalTo(lastBottomLabel.mas_right).offset(scaleLeftAndRight(0));
        make.height.mas_equalTo(1);
    }];
    if (@available(iOS 11.0, *)) {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
}
-(NSMutableAttributedString *)labelTextBack:(NSString *)text{
    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:text];
    [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, 4)];
    [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(5, attrStr.length - 5)];
    [attrStr addAttribute:NSForegroundColorAttributeName value:COLOR_HEX(0x121212) range:NSMakeRange(0, 4)];
    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, attrStr.length - 5)];
    return attrStr;
}
-(NSMutableAttributedString *)backFormatterAndStatement:(NSString *)text{
    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:text];
    [attrStr addAttribute:NSForegroundColorAttributeName value:COLOR_HEX(0xFFC921) range:NSMakeRange(5, 4)];
    [attrStr addAttribute:NSForegroundColorAttributeName value:COLOR_HEX(0xFFC921) range:NSMakeRange(attrStr.length - 6, 6)];
    return attrStr;
}
- (void)addBorderToLayer:(UIView *)view
{
    CAShapeLayer *border = [CAShapeLayer layer];
    //  线条颜色
    border.strokeColor = [UIColor lightGrayColor].CGColor;

    border.fillColor = nil;

    border.path = [UIBezierPath bezierPathWithRect:view.bounds].CGPath;

    border.path = [UIBezierPath bezierPathWithRoundedRect:view.bounds cornerRadius:0].CGPath;
    border.frame = view.bounds;

    // 不要设太大 不然看不出效果
    border.lineWidth = 1;

    border.lineCap = @"round";

    //  第一个是 线条长度   第二个是间距    nil时为实线
    border.lineDashPattern = @[@9, @4];

    [view.layer addSublayer:border];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
